// 公司管理

var constant = require('../../const');
var models = require('../../models');
var fs = require('fs');
var AV = require('leanengine');
var multiparty = require('multiparty');


function writeAritcleIndex(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}
	res.render('admin/homeArticle/write', {
		"username": req.session.username,
		"avatar": req.session.useravatar
	});
}

function uploadArticleImg(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}
	var form = new multiparty.Form();
	form.parse(req, function(err, fields, files) {
		var img = files.img[0];
		// 上传图片
		if (img.size !== 0) {
			// 要求上传图片
			fs.readFile(img.path, function(err, imgData) {
				if (err) {
					var data = {
						"username": req.session.username,
						"avatar": req.session.useravatar,
						"errMsg": "图片保存失败, 请重试",
					};
					res.render('admin/homeArticle/write', data);
				} else {
					// 保存图片
					var base64Data = imgData.toString('base64');
					var theFile = new AV.File(img.originalFilename, {
						base64: base64Data
					});
					theFile.save().then(function(file) {
						res.send({
							"code": 0,
							"url": file._url
						});

					}, function(error) {
						// 文件保存错误
						var data = {
							"username": req.session.username,
							"avatar": req.session.useravatar,
							"errMsg": "图片保存失败, 请重试",
						};
						res.render('admin/homeArticle/write', data);

					});
				}

			});

		}

	})
}

function addArticle(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var title = req.body.title;
	var name = req.body.name;
	var content = req.body.content;
	var article = new models.homeArticle;
	article.set("title", title);
	article.set("name", name);
	article.set("content", content);
	article.set("status", 0);
	article.fetchWhenSave(true);
	article.save(null, {
		success: function(article) {
			article.set("addr", "http://www.wudutuan.com/home/article?id=" + article.id);
			article.save();
			res.send({
				"code": 0,
				"msg": "成功"
			});
		},
		error: function(article, error) {
			res.send({
				"code": 0,
				"msg": "失败"
			});
		}
	})
}

function getSimpleArticle(req, res) {
	var id = req.query.id;
	var articlequery = new AV.Query(models.homeArticle);
	articlequery.get(id, {
		success: function(article) {
			var data = {
				"article": article
			}
			res.render('admin/homeArticle/homeArticleView', {
				data: data
			});
		},
		error: function(error) {
			//res.success(error);
		}
	})
}

function getArticle(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	// 0是正在编辑的, 1 已经发布的, 2 已经禁用了的
	//	var status = req.query.status || 0;
	var query = new AV.Query(models.homeArticle);
	//	if (constant.ARTICLE_STATUS_EDITING == status) {
	//		query.equalTo("status", constant.ARTICLE_STATUS_EDITING);
	//	} else if (constant.ARTICLE_STATUS_PUBLISHED == status) {
	//		query.equalTo("status", constant.ARTICLE_STATUS_PUBLISHED);
	//	} else if (constant.ARTICLE_STATUS_DISABLED == status) {
	//		query.equalTo("status", constant.ARTICLE_STATUS_DISABLED);
	//	}
	//	query.include("author");
	//	query.include("topic");
	//	query.descending("updatedAt");

	// 只看自己编辑的文章
	/*
	var authorQuery = new AV.Query(models.MLUser);
	authorQuery.equalTo("objectId", req.AV.user.id);
	query.matchesQuery("author", authorQuery);
	*/

	query.find({
		success: function(articles) {
			var data = {
				"username": req.session.username,
				"avatar": req.session.useravatar,
				"articles": articles
			};
			res.render('admin/homeArticle/index', data);
		},
		error: function(error) {
			res.render('admin/homeArticle/index', {
				"username": req.session.username,
				"avatar": req.session.useravatar,
			});
		}
	});

}

function editArticleIndex(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}
	var id = req.query.id;
	var articlequery = new AV.Query(models.homeArticle);
	articlequery.get(id, {
		success: function(article) {
			var data = {
				"article": article,
				"username": req.session.username
			}
			res.render('admin/homeArticle/write', data);
		},
		error: function(error) {
			//res.success(error);
		}
	})
}

function editArticle(req, res) {
	if (!req.AV.user) {
		res.redirect('/admin');
	}

	var title = req.body.title;
	var name = req.body.name;
	var content = req.body.content;
	var id = req.body.id;
	
	var article = new models.homeArticle;
	article.set("objectId",id);
	article.set("title", title);
	article.set("name", name);
	article.set("content", content);
	article.set("status", 0);
	article.fetchWhenSave(true);
	article.save(null, {
		success: function(article) {
			res.send({
				"code": 0,
				"msg": "成功"
			});
		},
		error: function(article, error) {
			res.send({
				"code": 0,
				"msg": "失败"
			});
		}
	})
}

exports.writeAritcleIndex = writeAritcleIndex;
exports.uploadArticleImg = uploadArticleImg;
exports.addArticle = addArticle;
exports.getSimpleArticle = getSimpleArticle;
exports.getArticle = getArticle;
exports.editArticleIndex = editArticleIndex;
exports.editArticle = editArticle;